/******************************************************************************************************
Copyright (C) 2019 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/

/*globals BaseConverter, NMRSpectrum, Molecule, Env, mainWindow, Dir, NMReDATAExporter*/
/*jslint plusplus: true, indent: 4*/

/** Default constructor of the converter
 * Defines the file filter
*/
function NMReDATAConverter() {
	"use strict";
	BaseConverter.call(this);
	this.exportFileFilter = "NMReDATA (*.zip)"; //!< It will override any other script converter with the same exportFileFilter
}
NMReDATAConverter.prototype = new BaseConverter();


NMReDATAConverter.prototype.wantsExport = function () {
	"use strict";

	var i, spectrum, linkedMols, mol,
		pageItems = mainWindow.activeDocument.pageItems;

	for (i = 0; i < pageItems.length; i++) {
		if (pageItems[i].name === "NMR Spectrum") {
			spectrum = new NMRSpectrum(pageItems[i]);
			if (spectrum.isValid()) {
				linkedMols = spectrum.getNMRAssignedMolecules();
				if (linkedMols.length) {
					mol = new Molecule(linkedMols[0]);
					if (mol.isValid()) {
						return true;
					}
				}
			}
		} else if (pageItems[i].name === "Molecule") {
			mol = new Molecule(pageItems[i]);
			if (mol.isValid()) {
				return true;
			}
		}
	}
	return false;
};


NMReDATAConverter.prototype.exportItems = function (notUsedItems, aFileName) {
	"use strict";

	var exporter, result;

	exporter = new NMReDATAExporter(aFileName);
	result = exporter.exportNMReDATA();
	return result;
};


/** Add to the Env.Converters environment engine
 */
Env.Converters.push(new NMReDATAConverter());

//test
function testNMReDATAConverter() {
	"use strict";
	var retVal = -1,
		NMReDataConv = new NMReDATAConverter();

	if (NMReDataConv.wantsExport()) {
		retVal = NMReDataConv.exportItems(undefined, Dir.home() + "/test.zip");
	}
	return retVal;
}
